package com.news.servlet;

import com.news.model.News;
import com.news.service.NewsService;
import com.news.service.impl.NewsServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/internationalNews")
public class InternationalNewsServlet extends HttpServlet {
    private NewsService newsService = new NewsServiceImpl();
    private static final int SORT_ID = 3; // 国际要闻
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            
            // 获取国际新闻列表
            List<News> newsList = newsService.getNewsList(SORT_ID, 14);
            // 获取热门新闻
            List<News> hotNews = newsService.getHotNews(SORT_ID);
            
            // 将数据存入request域
            request.setAttribute("newsList", newsList);
            request.setAttribute("hotNews", hotNews);
            
            // 在转发前添加
            request.setAttribute("currentPage", "internationalNews");
            
            // 转发到国际要闻页面
            request.getRequestDispatcher("internationalNews.jsp").forward(request, response);
        } catch (Exception e) {
            request.setAttribute("message", "系统错误：" + e.getMessage());
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
    }
} 